package com.zhoug.ocr.textin.parser;


import com.zhoug.common.utils.LogUtils;
import com.zhoug.ocr.textin.TextInUtils;
import com.zhoug.ocr.textin.data.BusinessLicense;
import com.zhoug.ocr.textin.data.BusinessResult;

import java.util.List;

/**
 * 营业执照识别结果格式化
 *
 * @Author: zhoug
 * @Date: 2023/3/3
 * @Description:
 */
public class BusinessResultParser {
    private static final String TAG = ">>>BusinessResultParser";

    public BusinessLicense parseBusinessLicense(BusinessResult result) {
        LogUtils.d(TAG, "parseBusinessLicense:" + result);
        BusinessLicense businessLicense = null;
        if (result != null) {
            businessLicense = new BusinessLicense();
            List<BusinessResult.Item> item_list = result.getItem_list();
            if (item_list != null && item_list.size() > 0) {
                for (BusinessResult.Item item : item_list) {
                    String key = item.getKey();
                    String value = item.getValue();
                    value = TextInUtils.remoteStartColon(value);
                    if ("BizLicenseIsCopy".equalsIgnoreCase(key)) {
                        businessLicense.setIsCopy(value);//是否为副本
                    } else if ("BizLicenseIsElectronic".equalsIgnoreCase(key)) {
                        businessLicense.setIsElectronic(value);//是否为电子营业执照
                    } else if ("BizLicenseSerialNumber".equalsIgnoreCase(key)) {
                        businessLicense.setLicenseCode(value);//证照编号
                    } else if ("BizLicenseRegistrationCode".equalsIgnoreCase(key)) {
                        businessLicense.setRegisterCode(value);//注册号
                    } else if ("BizLicenseRegCapital".equalsIgnoreCase(key)) {
                        businessLicense.setRegCapital(value);//注册资本
                    } else if ("BizLicenseCreditCode".equalsIgnoreCase(key)) {
                        businessLicense.setUscc(value);//统一社会信用代码
                    } else if ("BizLicenseCompanyName".equalsIgnoreCase(key)) {
                        businessLicense.setName(value);//名称
                    } else if ("BizLicenseOwnerName".equalsIgnoreCase(key)) {
                        businessLicense.setOwnerName(value);//经营者
                    } else if ("BizLicenseCompanyType".equalsIgnoreCase(key)) {
                        businessLicense.setType(value);//类型
                    } else if ("BizLicenseAddress".equalsIgnoreCase(key)) {
                        businessLicense.setAddress(value);//经营场所
                    } else if ("BizLicenseScope".equalsIgnoreCase(key)) {
                        businessLicense.setScope(value);//经营范围
                    } else if ("BizLicenseStartTime".equalsIgnoreCase(key)) {
                        businessLicense.setStartTime(TextInUtils.formatDate(value));//注册日期
                    } else if ("BizLicenseOperatingPeriod".equalsIgnoreCase(key)) {
                        if (value != null && value.contains("至")) {
                            try {
                                String[] split = value.split("至");
                                if(split.length>=2){
                                    businessLicense.setOperatingPeriod(TextInUtils.formatDate(split[0])+"至"+TextInUtils.formatDate(split[1]));
                                    String expireDate=split[1];
                                    if(expireDate.contains("长期")){
                                        businessLicense.setExpireDate("3000-01-01");
                                    }else{
                                        businessLicense.setExpireDate(TextInUtils.formatDate(expireDate));
                                    }
                                }

                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }else{
                            businessLicense.setOperatingPeriod(value);//营业期限
                        }

                    } else if ("BizLicenseComposingForm".equalsIgnoreCase(key)) {
                        businessLicense.setComposingForm(value);//组成形式
                    } else if ("BizLicensePaidInCapital".equalsIgnoreCase(key)) {
                        businessLicense.setPaidInCapital(value);//实收资本
                    } else if ("BizLicenseRegistrationDate".equalsIgnoreCase(key)) {
                        businessLicense.setRegistrationDate(TextInUtils.formatDate(value));//登记日期
                    }
                }
            }
        }
        return businessLicense;
    }
}
